<?php

class Comment extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'tbl_comment':
	 * @var integer $id
	 * @var string $content
	 * @var integer $status
	 * @var integer $created
	 * @var string $author
	 * @var string $email
	 * @var string $url
	 * @var integer $article_id
	 */
	const STATUS_PENDING=1;
	const STATUS_APPROVED=2;

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{comments}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('content', 'required'),
			array('author, email, url', 'length', 'max'=>128),
			array('email','email'),
			array('url','url'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'articles' => array(self::BELONGS_TO, 'Article', 'article_id'),
			'users' => array(self::BELONGS_TO, 'User', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'content' => 'Comment',
			'status' => 'Status',
			'created' => 'Create Time',
			'author' => 'Name',
			'email' => 'Email',
			'url' => 'Website',
			'article_id' => 'Article',
			'user_id' => 'User',
		);
	}

	public function search() {
		$criteria = new CDbCriteria;

		$criteria->compare('id', $this->id);
		$criteria->compare('article_id', $this->article_id);
		$criteria->compare('content', $this->content, true);
		$criteria->compare('email', $this->email);
		$criteria->compare('created', $this->created, true);
		$criteria->compare('url', $this->url, true);
		$criteria->compare('status', $this->status);
        $criteria->order = 't.created desc';
		/*
		$criteria->compare('articles.title', $this->articles, true, 'OR');
		$criteria->with = array('articles');
		*/
		$criteria->compare('users.username', $this->users, true, 'OR');
		$criteria->with = array('users');
		
		$criteria->together = true;
		
		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
		));
	}
	/**
	 * Approves a comment.
	 */
	public function approve()
	{
		$this->status=Comment::STATUS_APPROVED;
		$this->update(array('status'));
	}

	/**
	 * @param Article the article that this comment belongs to. If null, the method
	 * will query for the article.
	 * @return string the permalink URL for this comment
	 */
	public function getUrl($article=null)
	{
		if($article===null)
			$article=$this->article;
		return $article->url.'#c'.$this->id;
	}

	/**
	 * @return string the hyperlink display for the current comment's author
	 */
	public function getAuthorLink()
	{
		if(!empty($this->url))
			return CHtml::link(CHtml::encode($this->author),$this->url);
		else
			return CHtml::encode($this->author);
	}

	/**
	 * @return integer the number of comments that are pending approval
	 */
	public function getPendingCommentCount()
	{
		return $this->count('status='.self::STATUS_PENDING);
	}

	/**
	 * @param integer the maximum number of comments that should be returned
	 * @return array the most recently added comments
	 */
	public function findRecentComments($limit=10)
	{
		return $this->with('article')->findAll(array(
			'condition'=>'t.status='.self::STATUS_APPROVED,
			'order'=>'t.created DESC',
			'limit'=>$limit,
		));
	}

	/**
	 * This is invoked before the record is saved.
	 * @return boolean whether the record should be saved.
	 */
	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
				$this->created=time();
			return true;
		}
		else
			return false;
	}
	
	public function getAuthorName($user_id=Null) {
		if(is_null($user_id))	$user_id=$this->user_id;
		if ($userModel=User::Model()->findByAttributes(array('id'=>$user_id))) {
			$username = $userModel->username;
		}
		return(isset($username)) ? ($username) : "-----";
	}
	
	public function limit_text($text, $limit) {
		if (str_word_count($text, 0) > $limit) {
			$words = str_word_count($text, 2);
			$pos = array_keys($words);
			$text = substr($text, 0, $pos[$limit]) . '...';
		}
		return $text;
    }
}